package cn.interestingshop.service.user;

import cn.interestingshop.dao.UserDao;
import cn.interestingshop.dao.UserDaoImpl;
import cn.interestingshop.entity.User;
import cn.interestingshop.utils.DataSourceUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Array;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName : UserServiceImpl
 * @Description :
 * @Author : Lzz
 * @Date: 2023/2/20 14:51:42
 */
public class UserServiceImpl implements UserService {

    @Override
    public List<User> findAllUserList(Integer currentPageNo, Integer pageSize) {
        Connection conn = null;
        List<User> userList = new ArrayList<>();
        try {
            //1、获取Connection对象
            conn = DataSourceUtil.openConnection();
            //2、将获取到的Connection对象注入DAO层
            UserDao userDao = new UserDaoImpl(conn);
            //3、调用查询方法
            userList = userDao.findAllUserList(currentPageNo, pageSize);
        }catch (Exception ex){
            ex.printStackTrace();
        }finally {
            //关闭连接
            DataSourceUtil.closeConnection(conn);
        }
        return userList;
    }

    /**
     * 获取用户总记录条数
     * @return
     */
    @Override
    public int findUserCount() {
        Connection conn = null;
        int count = 0;      //总记录条数
        try {
            conn = DataSourceUtil.openConnection();
            UserDao userDao = new UserDaoImpl(conn);
            count = userDao.findUserCount();
        }catch (Exception ex){
            ex.printStackTrace();
        }finally {
            DataSourceUtil.closeConnection(conn);
        }
        return count;
    }

    @Override
    public boolean selectOneAccount(String account) {
        Connection conn=null;
        int rs=0;
        try {
            conn=DataSourceUtil.openConnection();
            UserDao userDao=new UserDaoImpl(conn);
            rs=userDao.selectOneAccount(account);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DataSourceUtil.closeConnection(conn);
        }
        return rs>0;
    }

    @Override
    public boolean selectOnePhone(String phone) {
        Connection conn=null;
        int rs=0;
        try {
            conn=DataSourceUtil.openConnection();
            UserDao userDao=new UserDaoImpl(conn);
            rs=userDao.selectOnePhone(phone);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DataSourceUtil.closeConnection(conn);
        }
        return rs>0;
    }

    @Override
    public User selectOne(Integer id) {
        Connection conn=null;
        User user=new User();

        try {
            conn=DataSourceUtil.openConnection();
            UserDao userDao=new UserDaoImpl(conn);
            user=userDao.selectOne(id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DataSourceUtil.closeConnection(conn);
        }

        return user;
    }

    @Override
    public int update(User user) {
        Connection conn=null;
        int update=0;
        try {
            conn=DataSourceUtil.openConnection();
            UserDao userDao=new UserDaoImpl(conn);
            update= userDao.update(user);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DataSourceUtil.closeConnection(conn);
        }


        return update;
    }

    @Override
    public int delete(Integer id) {
        Connection conn=null;
        int delete=0;
        try {
            conn=DataSourceUtil.openConnection();
            UserDao userDao=new UserDaoImpl(conn);
            delete= userDao.delete(id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DataSourceUtil.closeConnection(conn);
        }


        return delete;
    }

    @Override
    public int selectOne(String account, String password) {
        Connection conn=null;
        int rs=0;
        try {
            conn=DataSourceUtil.openConnection();
            UserDao userDao=new UserDaoImpl(conn);
            rs=userDao.selectOne(account,password);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DataSourceUtil.closeConnection(conn);
        }
        return rs;
    }

    @Override
    public int insert(User user) {
        Connection conn=null;
        int rs=0;
        try {
            conn=DataSourceUtil.openConnection();
            UserDao userDao=new UserDaoImpl(conn);
            rs=userDao.insert(user);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DataSourceUtil.closeConnection(conn);
        }
        return rs;
    }

    @Override
    public int selectOne(String phone) {
        Connection conn=null;
        int rs=0;
        try {
            conn=DataSourceUtil.openConnection();
            UserDao userDao=new UserDaoImpl(conn);
            rs=userDao.selectOne(phone);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DataSourceUtil.closeConnection(conn);
        }
        return rs;
    }


}
